﻿using Domain.Entity;
using DtoFw;
using DtoFw.Query;
using DtoFw.Result;
using Repository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Util;
using System.Linq;
using Service.Base;
using SqlSugar;
using DtoFw.Enum;
using DtoFw.Base;
using DtoFw.Interface;
using static Dapper.SqlMapper;

namespace Service
{
    public class CaseService : BaseService
    {
        private readonly CaseRepository _caseRepository;
        private readonly SnowFlakeGenerator _snowFlakeGenerator;
        private readonly CasePhysicalEvidenceRepository _casePhysicalEvidenceRepository;
        private readonly CaseAttachFileRepository _caseAttachFileRepository;

        public CaseService(CaseRepository caseRepository, SnowFlakeGenerator snowFlakeGenerator,
            CasePhysicalEvidenceRepository casePhysicalEvidenceRepository, CaseAttachFileRepository caseAttachFile,
            MainSerialNumberRepository mainSerialNumberRepository) : base(mainSerialNumberRepository)
        {
            this._caseRepository = caseRepository;
            this._casePhysicalEvidenceRepository = casePhysicalEvidenceRepository;
            this._snowFlakeGenerator = snowFlakeGenerator;
            this._caseAttachFileRepository = caseAttachFile;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public BaseResult Post(CasePostReqDto reqDto)
        {
            var entity = AutoMapperExtension.MapTo<CasePostReqDto, CaseEntity>(reqDto);
            entity.Creator = reqDto.UserName;
            entity.CreatorId = reqDto.UserId;
            entity.OrganId = reqDto.OrganId;
            if (_caseRepository.Insert(entity) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("新增案件失败");
        }
        /// <summary>
        /// 新增案件和物品
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        public BaseGenericResult<long> PostCaseEvidence(CaseEvidencePostReqDto reqDto)
        {
            var entity = AutoMapperExtension.MapTo<CaseEvidencePostReqDto, CaseEntity>(reqDto);
            entity.Creator = reqDto.UserName;
            entity.CreatorId = reqDto.UserId;
            entity.OrganId = reqDto.OrganId;
            _caseRepository.GetDb().BeginTran();
            long caseId = _caseRepository.Insert(entity);
            if (caseId < 1)
                return BaseGenericResult<long>.Failure(0, "新增案件失败");
            var evevidences = AutoMapperExtension.MapTo<EvidencePostReqDto, CasePhysicalEvidenceEntity>(reqDto.evidences);
            evevidences.ForEach(x =>
            {
                x.CaseId = caseId;
                x.CreatorId = entity?.CreatorId;
                x.Creator = entity?.Creator;
                x.CreateTime = entity.CreateTime;
                x.No = base.GetNumber(reqDto.OrganId, DtoFw.Enum.EnumSystemNumber.WZBH);
                x.OrganId = reqDto.OrganId;
                x.Status = (int)EnumEvidenceStatus.DJ;
                x.OrganName = reqDto.OrganName;
                x.DeptId = reqDto.DeptId;
                x.ComNo = reqDto.ComNo;
                x.UserId = reqDto.UserId;
            });
            if (_casePhysicalEvidenceRepository.InsertBatch(evevidences) < 1)
                return BaseGenericResult<long>.Failure(0, "新增案件物品失败");
            _caseRepository.GetDb().CommitTran();
            return BaseGenericResult<long>.Succeed(caseId);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public BaseResult Put(CasePutReqDto reqDto)
        {
            var entity = _caseRepository.GetDb().Queryable<CaseEntity>().First(x => x.Id == reqDto.Id);
            if (entity == null)
                return BaseResult.Failure("未查询到指定案件");
            entity = AutoMapperExtension.MapTo<CasePutReqDto, CaseEntity>(reqDto, entity);
            entity.Modifyer = reqDto.UserName;
            entity.ModifyerId = reqDto.UserId;
            if (_caseRepository.Save(entity).Id > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("案件信息修改失败");
        }
        /// <summary>
        /// 单个查询
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<CaseRespDto> Get(BaseQuery reqDto)
        {
            var entity = _caseRepository.GetDb().Queryable<CaseEntity>().First(x => x.Id == reqDto.Id);
            if (entity == null)
                return BaseGenericResult<CaseRespDto>.Failure(new CaseRespDto(), "未查询到指定案件");
            return BaseGenericResult<CaseRespDto>.Succeed(AutoMapperExtension.MapTo<CaseEntity, CaseRespDto>(entity));
        }

        public CaseEntity Get(string caseName,string caseNo)
        {
            var entity = _caseRepository.GetDb().Queryable<CaseEntity>().First(x => x.CaseName.Length > 0 && x.CaseName == caseName);
            if (entity == null)
                entity = _caseRepository.GetDb().Queryable<CaseEntity>().First(x => x.CaseNo.Length > 0 && x.CaseNo == caseNo);
            return entity;
        }

        /// <summary>
        /// 查询案件和案件物品
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<CaseEvidenceRespDto> GetCaseEvidence(long caseid)
        {
            var entity = _caseRepository.GetDb().Queryable<CaseEntity>().First(x => x.Id == caseid && !x.IsDeleted);
            if (entity == null)
                return BaseGenericResult<CaseEvidenceRespDto>.Failure(new CaseEvidenceRespDto(), "未查询到指定案件");
            var respDto = AutoMapperExtension.MapTo<CaseEntity, CaseEvidenceRespDto>(entity);
            var list = _casePhysicalEvidenceRepository.Query(new BaseQuery(), t => t.Where(m => m.CaseId == entity.Id && !m.IsDeleted))?.ToList();
            respDto.Evidences = AutoMapperExtension.MapTo<CasePhysicalEvidenceEntity, EvidenceRespDto>(list);
            respDto.DeliveryNo = "";
            return BaseGenericResult<CaseEvidenceRespDto>.Succeed(respDto);
        }
        /// <summary>
        /// 查询案件和案件物品
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<CaseEvidenceRespDto> GetEvidenceByIds(BaseQuery reqDto)
        {
            var respDto = new CaseEvidenceRespDto();

            var list = _casePhysicalEvidenceRepository.Query(new BaseQuery(), v => v.Where(t => reqDto.Ids.Contains(t.CaseId)))?.ToList();
            respDto.Evidences = AutoMapperExtension.MapTo<CasePhysicalEvidenceEntity, EvidenceRespDto>(list);
            respDto.DeliveryNo = "";
            return BaseGenericResult<CaseEvidenceRespDto>.Succeed(respDto);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<Pager<CaseRespDto>> Pager(CaseQueryDto query)
        {
            int total = 0;
            var list = _caseRepository.Query(new BaseQuery(),
                t => t.Where(x => !x.IsDeleted && (x.OrganId == query.OrganId))
                .WhereIF(!string.IsNullOrWhiteSpace(query.CaseNo), x => x.CaseNo.Contains(query.CaseNo)))
                .WhereIF(!string.IsNullOrWhiteSpace(query.InspectionNo), x => x.InspectionNo.Contains(query.InspectionNo))
                .WhereIF(!string.IsNullOrWhiteSpace(query.CaseName), x => x.CaseName.Contains(query.CaseName))
                .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserName), x => x.HostUserName == query.HostUserName)
                .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserOrganName), x => x.HostUserOrganName == (query.HostUserOrganName))
                .WhereIF((query.CaseTimeStart.HasValue && query.CaseTimeEnd.HasValue), x => (x.CaseTime > query.CaseTimeStart && x.CaseTime < query.CaseTimeEnd))
                .WhereIF(!string.IsNullOrWhiteSpace(query.Status), x => x.Id == SqlFunc.Subqueryable<CasePhysicalEvidenceEntity>().Where(e => e.CaseId == x.Id && !e.IsDeleted && e.Status == 1).Select(e => e.CaseId))
                .OrderBy(x => x.CreateTime, OrderByType.Desc)
                .ToPageList(query.Page, query.PageSize, ref total);
            BaseGenericResult<Pager<CaseRespDto>> baseGeneric = new BaseGenericResult<Pager<CaseRespDto>>();
            baseGeneric.Data = new Pager<CaseRespDto>();
            baseGeneric.Data.Data = AutoMapperExtension.MapTo<CaseEntity, CaseRespDto>(list);
            baseGeneric.Data.TotalCount = total;
            return baseGeneric;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<Pager<CaseRespDto>> PagerCaseTransfere(CaseTransfereQueryDto query)
        {
            int total = 0;
            int status1 = 1;
            int status2 = 2;
            if (query.DeliveryDicType == 6)
            {
                status1 = 2;
                status2 = 1;
            }
            BaseGenericResult<Pager<CaseRespDto>> baseGeneric = new BaseGenericResult<Pager<CaseRespDto>>();
            baseGeneric.Data = new Pager<CaseRespDto>();
            if (query.ActiveName == "first")
            {
                var caseIds = _caseRepository.GetDb().Queryable<CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity>((x, y) =>
                       new JoinQueryInfos(JoinType.Left, x.Id == y.EvidenceId))
                      .Where((x, y) => x.OrganId == query.OrganId && !x.IsDeleted && !y.IsDeleted && y.DeliveryDicType != query.DeliveryDicType && x.Status == status1)
                      .Select(x => x.CaseId)?.ToList();
                var list = _caseRepository.GetDb().Queryable<CaseEntity>()
                    .Where((x) => !x.IsDeleted && caseIds.Contains(x.Id))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.CaseNo), (x) => x.CaseNo.Contains(query.CaseNo))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.InspectionNo), (x) => x.InspectionNo.Contains(query.InspectionNo))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.CaseName), (x) => x.CaseName.Contains(query.CaseName))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserName), (x) => x.HostUserName == query.HostUserName)
                    .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserOrganName), (x) => x.HostUserOrganName == (query.HostUserOrganName))
                    .WhereIF((query.CaseTimeStart.HasValue && query.CaseTimeEnd.HasValue), (x) => (x.CaseTime >= query.CaseTimeStart && x.CaseTime <= query.CaseTimeEnd))
                    .OrderBy(x => x.CreateTime, OrderByType.Desc)
                    .ToPageList(query.Page, query.PageSize, ref total);
                baseGeneric.Data.Data = AutoMapperExtension.MapTo<CaseEntity, CaseRespDto>(list.Distinct());
                baseGeneric.Data.TotalCount = total;
            }
            else
            {
                var pager = _caseRepository.GetDb().Queryable<CaseEntity, CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity>((x, y, z) =>
                      new JoinQueryInfos(JoinType.Left, x.Id == y.CaseId, JoinType.Left, y.Id == z.EvidenceId))
                    .Where((x, y, z) => x.OrganId == query.OrganId && !x.IsDeleted && !y.IsDeleted && !z.IsDeleted && z.DeliveryDicType == query.DeliveryDicType && y.Status == status2)
                    .WhereIF(!string.IsNullOrWhiteSpace(query.CaseNo), (x) => x.CaseNo.Contains(query.CaseNo))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.InspectionNo), (x) => x.InspectionNo.Contains(query.InspectionNo))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.CaseName), (x) => x.CaseName.Contains(query.CaseName))
                    .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserName), (x) => x.HostUserName == query.HostUserName)
                    .WhereIF(!string.IsNullOrWhiteSpace(query.HostUserOrganName), (x) => x.HostUserOrganName == (query.HostUserOrganName))
                    .WhereIF((query.CaseTimeStart.HasValue && query.CaseTimeEnd.HasValue), (x) => (x.CaseTime >= query.CaseTimeStart && x.CaseTime <= query.CaseTimeEnd))
                    .OrderBy((x, y, z) => z.CreateTime, OrderByType.Desc)
                    .Select((x, y, z) => new CaseRespDto()
                    {
                        Id = x.Id,
                        CaseNo = x.CaseNo,
                        InspectionNo = x.InspectionNo,
                        CaseName = x.CaseName,
                        CaseTime = x.CaseTime,
                        ActiveTime = z.DeliveryTime,
                        Activer = z.DeliveryDicType == 6 ? z.Transferor : z.Receiver,
                        ActiveStatus = z.DeliveryDicType,
                        ActiveStatusName = z.DeliveryDicTypeName,
                        EvidenceStatus = y.Status,
                        EvidenceId = y.Id,
                        TransfereId = z.Id,
                        CaseTypeCode = x.CaseTypeCode,
                        CaseTypeName = x.CaseTypeName,
                        HostUserName = x.HostUserName,
                        HostUserOrganName = x.HostUserOrganName,
                        CasePlace = x.CasePlace,
                        BriefCase = x.BriefCase,
                        DeliveryNo = z.DeliveryNo,
                        EvidenceName = y.Name
                    })
                    .ToPageList(query.Page, query.PageSize, ref total);
                baseGeneric.Data.Data = pager;
                baseGeneric.Data.TotalCount = total;
            }
            return baseGeneric;
        }
        /// <summary>
        /// 删除案件
        /// </summary>
        /// <returns></returns>
        public BaseResult Delete(DeleteDto query)
        {
            var _lst = _casePhysicalEvidenceRepository.
                  GetDb().Queryable<CasePhysicalEvidenceEntity>()
                  .Where(x => query.Ids.Contains(x.CaseId) && !x.IsDeleted).ToList();
            if (_lst.Any(x => x.Status == 1))
            {
                return BaseResult.Failure("不能删除已入库物品的案件");
            }
            foreach (var item in query.Ids)
            {
                var dellist = _casePhysicalEvidenceRepository.Query(new BaseQuery(),
                  t => t.Where(m => m.CaseId == item))?.ToList()?
                  .Select(x =>
                  {
                      x.IsDeleted = true;
                      return x;
                  })?.ToList();

                _caseRepository.GetDb().Updateable<CasePhysicalEvidenceEntity>()
                                       .SetColumns(x => new CasePhysicalEvidenceEntity() { IsDeleted = true })
                                       .Where(x => x.CaseId == item).ExecuteCommand();
                {
                    foreach (var eItem in dellist)
                    {
                        //删除交接记录
                        _casePhysicalEvidenceRepository.DeleteTrans(eItem.Id);
                    }
                }
            }

            if (_caseRepository.Delete(new Domain.Base.BaseOperate() { Ids = query.Ids }) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("删除失败");
        }
        /// <summary>
        /// Report top 
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<CaseHomeRespDto> GetHomeCaseReport(TokenData _user)
        {
            //只统计本级单位
            var orgIds = new List<long> { _user.OrganId };
            CaseHomeRespDto caseHomeRespDto = new CaseHomeRespDto();
            caseHomeRespDto.list = new List<CaseHomeResp>();
            DateTime startTime = DateTime.Now.Date;
            DateTime endTime = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "案件总数", //案件
                color = "#F95959",
                iconColor = "#F86C6B",
                icon = "iconfont icon-jinridaiban",
                titleNum = _caseRepository.GetDb().Queryable<CaseEntity>().Where(x => !x.IsDeleted && orgIds.Contains(x.OrganId))?.Count() ?? 0,
                tipNum = _caseRepository.GetDb().Queryable<CaseEntity>()
                    .Where(x => !x.IsDeleted && x.CreateTime > startTime && x.CreateTime < endTime && orgIds.Contains(x.OrganId))?.Count() ?? 0
            });
            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "案卷总数", //物品
                color = "#8595F4",
                iconColor = "#92A1F4",
                icon = "iconfont icon-AIshiyanshi",
                titleNum = _caseRepository.GetDb().Queryable<CasePhysicalEvidenceEntity, CaseEntity>((t1, t2) => new JoinQueryInfos(JoinType.Left,t1.CaseId == t2.Id)).Where((t1, t2) => !t1.IsDeleted && orgIds.Contains(t2.OrganId)).Count(),
                tipNum = _caseRepository.GetDb().Queryable<CasePhysicalEvidenceEntity, CaseEntity>((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.CaseId == t2.Id)).Where((t1, t2) => !t1.IsDeleted && t1.CreateTime > startTime && t1.CreateTime < endTime && orgIds.Contains(t2.OrganId)).Count()
            });
            List<long?> DeliveryDicTypes = new List<long?>() { 1, 2, 3, 5, 6, 7, 8 };
            var list = _caseRepository.GetDb().Queryable<CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity,CaseEntity>((t1, t2,t3) => new JoinQueryInfos(JoinType.Left,
                      t1.Id == t2.EvidenceId, JoinType.Left,t1.CaseId == t3.Id))
                    .Where((t1, t2,t3) => !t1.IsDeleted && !t2.IsDeleted && orgIds.Contains(t3.OrganId) && t2.HistoryStatus == 0 && DeliveryDicTypes.Contains(t2.DeliveryDicType))
                    .Select((t1, t2) => t2).ToList();
            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "已入库", //入库
                color = "#FEBB50",
                iconColor = "#FDC566",
                icon = "iconfont icon-shenqingkaiban",
                titleNum = list.Count(x => x.DeliveryDicType == 6 || x.DeliveryDicType == 7 || x.DeliveryDicType == 8),
                tipNum = list.Count(x => (x.DeliveryDicType == 6 || x.DeliveryDicType == 7 || x.DeliveryDicType == 8) && x.CreateTime > startTime && x.CreateTime < endTime)
            });
            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "已借调", //已借调
                color = "#63caff",
                iconColor = "#49beff",
                icon = "iconfont icon-diannao",
                titleNum = list.Count(x => x.DeliveryDicType == 1),
                tipNum = list.Count(x => x.DeliveryDicType == 1 && x.CreateTime > startTime && x.CreateTime < endTime),
            });

            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "已发还", //已送检
                color = "#6c93ee",
                iconColor = "#a9abff",
                icon = "iconfont icon-zhongduancanshu",
                titleNum = list.Count(x => x.DeliveryDicType == 3),
                tipNum = list.Count(x => x.DeliveryDicType == 3 && x.CreateTime > startTime && x.CreateTime < endTime),
            });

            //caseHomeRespDto.list.Add(new CaseHomeResp()
            //{
            //    title = "已销毁", //已移交
            //    color = "#54FF9F",
            //    iconColor = "#cb79ff",
            //    icon = "iconfont icon-shenqingkaiban",
            //    titleNum = list
            //        .Where(x => x.DeliveryDicType == 4).Sum(x => x.EvidenceQuantity) ?? 0,
            //    tipNum = list
            //        .Where(x => x.DeliveryDicType == 4 && x.CreateTime > startTime && x.CreateTime < endTime).Sum(x => x.EvidenceQuantity) ?? 0,
            //});

            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "已移交", //已移交
                color = "#bd74e0",
                iconColor = "#cb79ff",
                icon = "iconfont icon-quanxian",
                titleNum = list.Count(x => x.DeliveryDicType == 5),
                tipNum = list.Count(x => x.DeliveryDicType == 5 && x.CreateTime > startTime && x.CreateTime < endTime),
            });
         
            caseHomeRespDto.list.Add(new CaseHomeResp()
            {
                title = "已回库", //已回库
                color = "#38b99c",
                iconColor = "#49beff",
                icon = "iconfont icon-shoujidiannao",
                titleNum = list.Count(x => x.DeliveryDicType == 6),
                tipNum = list.Count(x => x.DeliveryDicType == 6 && x.CreateTime > startTime && x.CreateTime < endTime),
            });
            return BaseGenericResult<CaseHomeRespDto>.Succeed(caseHomeRespDto);
        }

        public BaseGenericResult<object> QueryCaseHistory(string JJBH, string ASJBH, string XCKYBH)
        {
            var list = this._caseRepository.GetThirdSystemCase(JJBH, ASJBH, XCKYBH);
            if(list == null || list.Count == 0)
            {
                return BaseGenericResult<object>.Failure("","");
            }
            return BaseGenericResult<object>.Succeed(list);
        }

        #region 案件附件
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<Pager<CaseAttachFileEntity>> QueryCaseAttach(CaseAttachReqDto query)
        {
            int total = 0;
            var list = _caseAttachFileRepository.QueryList(query, ref total);
            BaseGenericResult<Pager<CaseAttachFileEntity>> baseGeneric = new BaseGenericResult<Pager<CaseAttachFileEntity>>();
            baseGeneric.Data = new Pager<CaseAttachFileEntity>();
            baseGeneric.Data.Data = list;
            baseGeneric.Data.TotalCount = total;
            return baseGeneric;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<string> DeleteCaseAttachFile(CaseAttachReqDto query)
        {
            string ids = "";
            if (query.Ids.Count == 0)
            {
                ids = query.Id.ToString();
            }
            else
            {
                ids = string.Join(",", query.Ids);
            }
            string sql = $@"update case_attachfile set isdeleted=1,
                                      Modifyer='{query.CurrentUser.UserName}',
                                      ModifyerId='{query.CurrentUser.UserId}',
                                      ModifyerTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'
                              where id in({ids})";
            var ret = _caseAttachFileRepository.GetDb().Ado.ExecuteCommand(sql) > 0;
            return new BaseGenericResult<string>() { Success = ret, Msg = ret ? "删除成功" : "删除失败" };
        }
        /// <summary>
        /// 分页
        /// </summary>      
        /// <returns></returns>
        public BaseGenericResult<string> MatchEvidence(CaseAttachReqDto query)
        {
            var user = query.CurrentUser;
            var t = new CaseAttachFileEvidenceEntity()
            {
                caseId = Convert.ToInt64(query.caseid),
                evidenceId = Convert.ToInt64(query.evidenceId),
                CreateTime = DateTime.Now,
                Creator = user.UserName,
                CreatorId = user.UserId,
                Modifyer = user.UserName,
                ModifyerId = user.UserId,
                ModifyerTime = DateTime.Now,
            };
            foreach (var item in query.Ids)
            {
                t.attachId = item;
                if (!_caseAttachFileRepository.eviRef.IsAny(v => v.evidenceId == t.evidenceId && v.attachId == t.attachId && !v.IsDeleted))
                {
                    //不存在
                    _caseAttachFileRepository.eviRef.Insert(t);
                }
            }
            return new BaseGenericResult<string>() { Success = true, Msg = "关联成功" };
        }
        /// <summary>
        /// 分页
        /// </summary>      
        /// <returns></returns>
        public BaseGenericResult<string> CancelMatchEvidence(CaseAttachReqDto query)
        {
            string sql = $@"update case_attachfile_evidence set IsDeleted=1,
                                   Modifyer='{query.CurrentUser.UserName}',
                                   ModifyerId='{query.CurrentUser.UserId}',
                                   ModifyerTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'
                           where caseId={query.caseid} And attachId={query.attachId} ";
            if (!string.IsNullOrEmpty(query.evidenceId))
            {
                sql += " And evidenceId=" + query.evidenceId;
            }
            var ret = _caseAttachFileRepository.GetDb().Ado.ExecuteCommand(sql) > 0;
            return new BaseGenericResult<string>() { Success = ret, Msg = ret ? "取消关联成功" : "取消关联失败" };
        }
        #endregion 案件附件
    }
}
